package ipad.asia.controller;

import ipad.asia.entity.*;
import ipad.asia.service.bbs.BbsFeign;
import ipad.asia.service.consumer.ConsumerFeign;
import ipad.asia.service.product.ProductFeign;
import ipad.asia.service.shop.ShopFeign;
import ipad.asia.service.shopCart.ShopCartFeign;
import ipad.asia.utils.MyTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Controller
public class CartController {
    @Autowired
    private ShopCartFeign shopCartFeign;
    @Autowired
    private ConsumerFeign consumerFeign;
    @Autowired
    private ShopFeign shopFeign;
    @Autowired
    private ProductFeign productFeign;

    @RequestMapping("/addCart")
    public String addCart(@RequestParam(value = "productId", required = false) String productId,
                          @RequestParam(value = "shopId", required = false) String shopId,
                          HttpServletRequest request, RedirectAttributes redirectAttributes) {
        System.out.println("id========" + productId);
        System.out.println("cho_number========" + shopId);
        HttpSession session = request.getSession();
        System.out.println(session.getAttribute("user"));
        String userName = (String) session.getAttribute("user");
        if (userName == null) {
            return "/login";
        } else {
            session.setAttribute("detail_productId", productId);
            Consumer consumer = consumerFeign.getName((String) session.getAttribute("user"));
            System.out.println(consumer);
            List<ShoppingCart> shoppingCartList = shopCartFeign.getCart(consumer.getId());
            List<String> productIdList = new LinkedList<String>();
            for (int i = 0; i < shoppingCartList.size(); i++) {
                productIdList.add(shoppingCartList.get(i).getProductId() + "");
            }

            System.out.println("listcart" + productIdList);
            System.out.println("productIdList.contains(productId)" + productIdList.contains(productId));

            if (productIdList.contains(productId) == true) {
                redirectAttributes.addAttribute("message", "购物车已存在该商品");
                return "redirect:/detail";
            } else {
                shopCartFeign.CartAdd(consumer.getId(), Integer.parseInt(productId), Integer.parseInt(shopId), 1);
                redirectAttributes.addAttribute("message", "成功加入购物车");
            }
            return "redirect:/detail";
        }
    }

    @RequestMapping("/cart")
    public String cart(HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        System.out.println(session.getAttribute("user"));
        String userName = (String) session.getAttribute("user");
        List<ShoppingCart> cartList = shopCartFeign.getCart(consumerFeign.getName(userName).getId());

        List<Product> products = new LinkedList<>();
        for (int i = 0; i < cartList.size(); i++) {
            products.add(productFeign.getById(cartList.get(i).getProductId()));
        }
        System.out.println(products);
        model.addAttribute("products", products);
//            model.addAttribute("shops",shops);
        return "cart";
    }

    @RequestMapping("/cartDel")
    public String cartDel(HttpServletRequest request, Model model,
                          @RequestParam(value = "productId")int productId) {
        HttpSession session = request.getSession();
        System.out.println(session.getAttribute("user"));
        String userName = (String) session.getAttribute("user");
        shopCartFeign.deleteByProductIdAndConsumerId(productId,consumerFeign.getName(userName).getId());
        return "redirect:/cart";
    }
}